package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.dao.ContractActualSendDao;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.util.HttpHelper;
import com.gable.exhibition.view.ContractActualSendView;
import com.gable.exhibition.view.UserView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2018/3/28.
 * 发货指派给其他服务商之后，其他服务商指派工程师推送短信
 */
public class DispatchEngineerSmsThread implements Runnable{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private Long serviceType;       //服务商类型

    private Long serviceId;         //服务商ID

    private String manufacturerName;    //厂商名称

    private Long contractActualSendId;        //实际发货单ID

    private String estimateInstallDate;     //预计安装日期

    private String engineerName;        //工程师名称

    private String engineerPhone;       //工程师电话

    private Integer sendCountForSms;    //发货数量

    private ContractActualSendDao contractActualSendDao;

    private UserDao userDao;

    private String hospitalUrl; //发送给医院的短信URL

    private String engineerUrl; //发送给工程师的短信URL

    private String serviceUrl;  //发送给其他服务商的短信URL

    public DispatchEngineerSmsThread(Long serviceType,Long serviceId,String manufacturerName,Long contractActualSendId,
                                     String estimateInstallDate,String engineerName,String engineerPhone,
                                     Integer sendCountForSms,ContractActualSendDao contractActualSendDao,
                                     UserDao userDao,String hospitalUrl,String engineerUrl,String serviceUrl){
        this.serviceType = serviceType;
        this.serviceId = serviceId;
        this.manufacturerName = manufacturerName;
        this.contractActualSendId = contractActualSendId;
        this.estimateInstallDate = estimateInstallDate;
        this.engineerName = engineerName;
        this.engineerPhone = engineerPhone;
        this.sendCountForSms = sendCountForSms;
        this.contractActualSendDao = contractActualSendDao;
        this.userDao = userDao;
        this.hospitalUrl = hospitalUrl;
        this.engineerUrl = engineerUrl;
        this.serviceUrl = serviceUrl;
    }
    @Override
    public void run() {
        ContractActualSendView contractActualSendView = contractActualSendDao.queryContractActualSendSms(contractActualSendId);
        if(contractActualSendView == null){
            logger.info("发送指派工程师查询记录无效，发货单ID："+contractActualSendId);
            return;
        }
        String supplierName = contractActualSendView.getSupplierName();
        String contractNo = contractActualSendView.getContractNo();
        String serviceName = contractActualSendView.getServiceName();
        String hospitalName = contractActualSendView.getHospitalName();
        List<String> mobiles = contractActualSendView.getMobiles();
        //厂商
        if(serviceType.intValue() == 1102){
            try {
                StringBuilder sb = new StringBuilder();
                if(mobiles != null && mobiles.size() > 0){
                    for(String phone : mobiles){
                        if(StringUtil.isEmpty(phone))
                            continue;
                        sb.append(phone).append(",");
                    }
                    String mobile = sb.substring(0, sb.length() - 1);
                    String message = supplierName+"%&"+contractNo+"%&"+sendCountForSms+"%&"+serviceName+"%&"+engineerName+"%&"+estimateInstallDate;
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("mobile",mobile);
                    map.put("message",message);
                    String jsonResult = HttpHelper.postRequestResponseBodyAsString(hospitalUrl,map);
                    logger.info("合同编号为："+contractNo+"的发货指派工程师,短信发送给医院的结果:"+jsonResult);
                }
            } catch (Exception e) {
                logger.info("合同编号为："+contractNo+"的发货指派工程师,短信发送给医院失败:"+e);
            }
            if(!StringUtils.isEmpty(engineerPhone)){
                try {
                    String message = hospitalName+"%&"+contractNo;
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("mobile",engineerPhone);
                    map.put("message",message);
                    String jsonResult = HttpHelper.postRequestResponseBodyAsString(engineerUrl,map);
                    logger.info("合同编号为："+contractNo+"的发货指派工程师,短信发送给工程师的结果:"+jsonResult);
                } catch (Exception e) {
                    logger.info("合同编号为："+contractNo+"的发货指派工程师,短信发送给工程师失败:"+e);
                }
            }
        }
        //维修商
        else if(serviceType.intValue() == 1103){
            try {
                String message = hospitalName+"%&"+contractNo+"%&"+manufacturerName;
                UserView userView = new UserView();
                userView.setMaintainerId(serviceId);
                List<String> usersMobile = userDao.getUsersMobile(userView);
                StringBuilder sb = new StringBuilder();
                if(usersMobile != null && usersMobile.size() > 0){
                    for(String phone : usersMobile){
                        if(StringUtil.isEmpty(phone))
                            continue;
                        sb.append(phone).append(",");
                    }
                    String mobile = sb.substring(0, sb.length() - 1);
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("mobile",mobile);
                    map.put("message",message);
                    String jsonResult = HttpHelper.postRequestResponseBodyAsString(serviceUrl,map);
                    logger.info("合同编号为："+contractNo+"的发货指派维修商,短信发送给维修商的结果:"+jsonResult);
                }
            } catch (Exception e) {
                logger.info("合同编号为："+contractNo+"的发货指派维修商,短信发送给维修商失败:"+e);
            }
        }
    }
}
